/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.yongbao.controller.wechat;

import com.yongbao.common.WebResponse;
import com.yongbao.controller.BaseController;
import com.yongbao.model.request.manual.AuthLoginParam;
import com.yongbao.model.request.manual.UserUpdateRequest;
import com.yongbao.model.vo.UserVO;
import com.yongbao.model.vo.manual.GetPhoneNumberVO;
import com.yongbao.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhanghong
 * @version WechatUserController.java, v 0.1 2024/3/5 10:31 下午 WechatUserController Exp $$
 */
@RestController
@RequestMapping("/wechat/user")
@Api(value = "【小程序端】用户模块", tags = {"【小程序端】用户模块"})
public class WechatUserController extends BaseController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "授权登录", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/auth/login")
    public WebResponse<String> authLogin(@RequestBody AuthLoginParam param) {
        return userService.authLogin(param);
    }

    @ApiOperation(value = "退出登陆", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/auth/logout")
    public WebResponse<Boolean> authLogout(HttpServletRequest request) {
        boolean logout = logoutUser(request);
        return new WebResponse<>(logout);
    }

    @ApiOperation(value = "获取手机号", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @GetMapping("/getPhoneNumber")
    public WebResponse<GetPhoneNumberVO> getPhoneNumber(@RequestParam String code) {
        return userService.getPhoneNumber(code);
    }

    @ApiOperation(value = "更新", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/update")
    public WebResponse<Boolean> save(HttpServletRequest request, @RequestBody UserUpdateRequest wechatUser) {
        return new WebResponse<>(userService.updateByKey(wechatUser));
    }

    @ApiOperation(value = "获取个人信息", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @GetMapping("/me")
    public WebResponse<UserVO> me(HttpServletRequest request) {
        UserVO user = validUserLoginAndGet(request, UserVO.class);
        return new WebResponse<>(userService.getByKey(user.getId()));
    }
}
